package ex_from_web;


	import java.util.Date;
	import java.util.HashMap;
	import java.util.LinkedHashMap;
	import java.util.Map;
	import java.util.TreeMap;
	import java.util.Hashtable;


	public class perfomence_test_array_list {
	   static int iterations = 100;
	   static long addTime;
	   static long iterTimeKey;
	   static long iterTimeVal;
	   static long getTime;
	   static long delTime;
	    
	   /**
	    * Map performace test
	    * Speed test of HashMap, Hashtable, LinkedHashMap and TreeMap 
	    * Testing speed of: adding, removing and accessing objects from the map,
	    * and iterating through the map (keys and values)
	    */
	   public static void main(String[] args) {
	         HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
	         Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
	         LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>();
	         TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
	     
	     addTime = 0;
	     iterTimeKey = 0;
	     iterTimeVal = 0;
	     getTime = 0;
	     delTime = 0;
	     for (int i = 1; i < iterations ; i++) {
	         test(hashMap);
	     }
	     printReport(hashMap);
	     
	     addTime = 0;
	     iterTimeKey = 0;
	     iterTimeVal = 0;
	     getTime = 0;
	     delTime = 0;
	     for (int i = 1; i < iterations ; i++) {
	         test(linkedHashMap);
	     }
	     printReport(linkedHashMap);

	     addTime = 0;
	     iterTimeKey = 0;
	     iterTimeVal = 0;
	     getTime = 0;
	     delTime = 0;
	     for (int i = 1; i < iterations ; i++) {
	         test(hashTable);
	     }
	     printReport(hashTable);         
	     
	     addTime = 0;
	     iterTimeKey = 0;
	     iterTimeVal = 0;
	     getTime = 0;
	     delTime = 0;
	     for (int i = 1; i < iterations ; i++) {
	                test(treeMap);
	     }
	     printReport(treeMap);
	   }
	    
	   private static void test(Map<Integer, String> map) {
	     int size = 1000000;
	     //adding 1mil. objects to the map
	     long startTime = new Date().getTime();
	     for (int i=0; i<size; i++) {
	         map.put(i, "Test");
	     }
	     long endTime = new Date().getTime();
	     addTime += endTime - startTime;
	     
	     //iterating through the map keys
	     startTime = new Date().getTime();
	     for(Integer i : map.keySet()) {
	         
	     }
	     endTime = new Date().getTime(); 
	     iterTimeKey += endTime - startTime;
	     
	    //iterating through the map values
	     startTime = new Date().getTime();
	     for(String s : map.values()) {
	         
	     }
	     endTime = new Date().getTime(); 
	     iterTimeVal += endTime - startTime;
	     
	     //retrieving random 10000 objects from the map
	     startTime = new Date().getTime();
	     for (int i = 0; i < 10000; i++) {
	         String s = map.get((int)(Math.random() * size));
	     }
	     endTime = new Date().getTime();
	     getTime += endTime - startTime;
	     
	     //removing random 100000 objects from the map
	     startTime = new Date().getTime();
	     for (int i = 0; i < 100000; i++) {
	         map.remove(((int)(Math.random() * (size - i))));
	     }
	     endTime = new Date().getTime();
	     delTime += endTime - startTime;
	     map.clear();
	   }
	    
	   private static void printReport(Map<Integer, String> list) {
	    System.out.println(list.getClass().getSimpleName() + 
	        " - adding elements average time: " + Math.round(addTime / iterations) + " msec");
	    System.out.println(list.getClass().getSimpleName() + 
	        " - iteration average time (keys): " + Math.round(iterTimeKey / iterations) + " msec");
	    System.out.println(list.getClass().getSimpleName() + 
	        " - iteration average time (values): " + Math.round(iterTimeVal / iterations) + " msec");
	    System.out.println(list.getClass().getSimpleName() + 
	        " - retrieving random elements average time: " + Math.round(getTime / iterations) + " msec");
	    System.out.println(list.getClass().getSimpleName() + 
	        " - deleting random elements average time: " + Math.round(delTime / iterations) + " msec");
	   }
	}

